﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO;

namespace QuickResizer
{
	public class Resizers : Dictionary<int,ResizerItem>
	{
		private const string configurationFileName = "configuration.xml";

		public string SubFolder { get; set; }

		public void LoadFromConfiguration()
		{
			FileInfo fileInfo = new FileInfo(Application.StartupPath + "\\" + configurationFileName);
			if (fileInfo.Exists)
			{
				XmlDocument xml = new XmlDocument();

				try
				{
					xml.Load(Application.StartupPath + "\\" + configurationFileName);
				}
				catch (XmlException) { }

				if (xml.DocumentElement != null)
				{
					XmlNode subFolderNode = xml.DocumentElement.SelectSingleNode("SubFolder");
					SubFolder = subFolderNode.Attributes.GetNamedItem("value").Value;

					XmlNode resizerNode = xml.DocumentElement.SelectSingleNode("Resizers");
					if (resizerNode != null)
					{
						XmlNodeList list = resizerNode.ChildNodes;

						foreach (XmlNode node in list)
						{
							ResizerItem item = new ResizerItem();
							XmlAttributeCollection attrCol = node.Attributes;
							XmlAttribute attr;
							attr = (XmlAttribute)attrCol.GetNamedItem("name");
							item.Name = attr.Value;
							attr = (XmlAttribute)attrCol.GetNamedItem("index");
							int index;
							if (int.TryParse(attr.Value, out index))
							{
								item.Index = index;
							}
							attr = (XmlAttribute)attrCol.GetNamedItem("show");
							bool show;
							if (bool.TryParse(attr.Value, out show))
							{
								item.Show = show;
							}
							attr = (XmlAttribute)attrCol.GetNamedItem("width");
							if (!string.IsNullOrWhiteSpace(attr.Value))
							{
								item.Width = Convert.ToInt32(attr.Value);
							}
							attr = (XmlAttribute)attrCol.GetNamedItem("height");
							if (!string.IsNullOrWhiteSpace(attr.Value))
							{
								item.Height = Convert.ToInt32(attr.Value);
							}
							attr = (XmlAttribute)attrCol.GetNamedItem("prefix");
							item.Prefix = attr.Value;
							attr = (XmlAttribute)attrCol.GetNamedItem("suffix");
							item.Suffix = attr.Value;
							item.SubFolder = SubFolder;
							this.Add(item.Index, item);
						}
					}
				}
			}
		}

		public void SaveToConfiguration()
		{
			XmlTextWriter writer = new XmlTextWriter(Application.StartupPath + "\\" + configurationFileName, null);

			writer.Formatting = Formatting.Indented;
			writer.Indentation = 4;
			writer.Namespaces = false;

			writer.WriteStartDocument();

			writer.WriteStartElement("Configuration");

			writer.WriteStartElement("Resizers");

			int i = 0;
			foreach (ResizerItem item in this.Values)
			{
				writer.WriteStartElement("Resizer");
				writer.WriteStartAttribute("", "index", "");
				writer.WriteString(i.ToString());
				writer.WriteEndAttribute();
				writer.WriteStartAttribute("", "name", "");
				writer.WriteString(item.Name);
				writer.WriteEndAttribute();
				writer.WriteStartAttribute("", "show", "");
				writer.WriteString(item.Show.ToString());
				writer.WriteEndAttribute();
				writer.WriteStartAttribute("", "width", "");
				writer.WriteString(item.Width.ToString());
				writer.WriteEndAttribute();
				writer.WriteStartAttribute("", "height", "");
				writer.WriteString(item.Height.ToString());
				writer.WriteEndAttribute();
				writer.WriteStartAttribute("", "prefix", "");
				writer.WriteString(item.Prefix);
				writer.WriteEndAttribute();
				writer.WriteStartAttribute("", "suffix", "");
				writer.WriteString(item.Suffix);
				writer.WriteEndAttribute();
				writer.WriteEndElement();
				i++;
			}

			writer.WriteEndElement();

			writer.WriteStartElement("SubFolder");
			writer.WriteStartAttribute("", "value", "");
			writer.WriteString(SubFolder);
			writer.WriteEndAttribute();
			writer.WriteEndElement();

			writer.WriteEndElement();
			writer.Flush();
			writer.Close();

		}
	}
}
